package org.ravenhearte.hardboild.gamelogic.skills;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.ListIterator;

import org.ravenhearte.hardboild.gamelogic.SkillSet.Type;
import org.ravenhearte.hardboild.gamelogic.*;
import org.ravenhearte.hardboild.gui.Container;

import com.jme.renderer.ColorRGBA;

public class MoveSkill extends Skill {
	public MoveSkill() {
		super("Basic Move", Type.ACTIVE, OnSelect.TARGET, ColorRGBA.blue);
		setCost(5);
	}

	@Override
	public Container getOptions() {
		return null;
	}

	@Override
	public LinkedList<String> getTargetRange() {
		GameTile center = getParent().getParent().getTile();
		String centerKey = GameMap.intArrayToString(center.getLocation());
		GameMap map = center.getParent();
		HashMap<String, GameTile> tiles = map.getTiles();

		int steps = 4 + getRanking();
		LinkedList<String> possibleNeighbors = center.getPossibleNeighbors();
		LinkedList<String> neighbors = new LinkedList<String>();
		
		for(int i = 0; i < steps; i++) {
			ListIterator<String> it = possibleNeighbors.listIterator();
			LinkedList<String> recentNeighbors = new LinkedList<String>();
			while(it.hasNext()) {
				String tileKey = it.next();
				int[] tileLoc = GameMap.stringToIntArray(tileKey);
				tileLoc[2] -= 1;
				for(int j = 0; j < 3; j++) {
					int[] temp = tileLoc.clone();
					temp[2] += j;
					String tempKey = GameMap.intArrayToString(temp);
					if(tiles.containsKey(tempKey)) {
						if(!neighbors.contains(tempKey) &&
								tempKey.compareTo(centerKey) != 0  &&
								!tiles.get(tempKey).isOccupied()) {
							neighbors.add(tempKey);
							recentNeighbors.add(tempKey);
						}
					}
				}
			}
			
			possibleNeighbors = new LinkedList<String>();
			ListIterator<String> lit = recentNeighbors.listIterator();
			
			while(lit.hasNext()) {
				String neighborLoc = lit.next();
				GameTile temp = tiles.get(neighborLoc);
				possibleNeighbors.addAll(temp.getPossibleNeighbors());
			}
		}
		
		return neighbors;
	}

	@Override
	public void performSkill(GameTile tile) {
		GameCharacter character = getParent().getParent();
		GameMap map = tile.getParent();
		
		character.increaseWill(-getCost());
		map.walkCharacter(tile, character);
	}

	@Override
	public int getRanking() {
		int parentRating = getParent().getRating();
		int skillPoints = getSkillPoints();
		
		return (parentRating + skillPoints) / 5;
	}
}
